package com.offcn.sms.utils;

import com.offcn.utils.HttpUtils;
import org.apache.http.HttpResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class SmsUtil {

    @Value("${sms.appcode}")
    private String appcode;

    @Value("${sms.tpl_id}")
    private String tpl_id;

    /**
     * 短信发送
     * @param phone 发送手机号
     * @param code  发送的验证码
     */
    public HttpResponse sendSms(String phone,String code){
        String host = "http://dingxin.market.alicloudapi.com";
        String path = "/dx/sendSms";

        HashMap<String, String> headers = new HashMap<>();
        headers.put("Authorization",appcode);

        HashMap<String, String> querys = new HashMap<>();
        querys.put("mobile",phone);
        querys.put("param","code:" + code);
        querys.put("tpl_id",tpl_id);

        try {
            HttpResponse response = HttpUtils.doPost(host, path, "post", headers, querys, "");
            System.out.println(response.toString());
            return response;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
